﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using VietThinks.Contract;

namespace VietThinks.Website
{
    public class ApplicationSettingsManager
    {
        private ApplicationSettingsManager() { }
        private static ApplicationSettingsManager instance = new ApplicationSettingsManager();

        public static ApplicationSettingsManager Current
        {
            get { return instance; }
        }

        public void Reset()
        {
            HttpContext.Current.Application.RemoveAll();
            this.BuildConfiguration();
        }

        private void BuildConfiguration()
        {
            var service = ServiceProvider.GetService<IAdminService>();
            IList<ConfigurationDto> configurations = service.GetAllConfigurations();
            foreach (var c in configurations)
            {
                HttpContext.Current.Application.Add(c.Variable, c.Value);
            }
        }

        #region Configurations from Database
        public string EmailFrom
        {
            get
            {
                if (HttpContext.Current.Application["EMAIL_FROM"] == null)
                {
                    this.BuildConfiguration();
                }
                return HttpContext.Current.Application["EMAIL_FROM"].ToString();
            }
        }

        public string StoreOwnerEmail
        {
            get
            {
                if (HttpContext.Current.Application["STORE_OWNER_EMAIL_ADDRESS"] == null)
                {
                    this.BuildConfiguration();
                }
                return HttpContext.Current.Application["STORE_OWNER_EMAIL_ADDRESS"].ToString();
            }
        }

        public string EmailNotificationRecipient
        {
            get
            {
                if (HttpContext.Current.Application["EMAIL_NOTIFICATION_SENT_TO"] == null)
                {
                    this.BuildConfiguration();
                }
                return HttpContext.Current.Application["EMAIL_NOTIFICATION_SENT_TO"].ToString();
            }
        }

        public bool IsLiveChatEnable
        {
            get
            {
                if (HttpContext.Current.Application["ENABLE_LIVE_CHAT_SCRIPT"] == null)
                {
                    this.BuildConfiguration();
                }
                if (HttpContext.Current.Application["ENABLE_LIVE_CHAT_SCRIPT"].ToString() == "True")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public string StoreName
        {
            get
            {
                if (HttpContext.Current.Application["STORE_NAME"] == null)
                {
                    this.BuildConfiguration();
                }
                return HttpContext.Current.Application["STORE_NAME"].ToString();
            }
        }

        public string SendExtraOrderEmailsTo
        {
            get
            {
                if (HttpContext.Current.Application["SEND_EXTRA_ORDER_EMAILS_TO"] == null)
                {
                    this.BuildConfiguration();
                }
                return HttpContext.Current.Application["SEND_EXTRA_ORDER_EMAILS_TO"].ToString();
            }
        }

        public string OmnitureAccount
        {
            get
            {
                if (HttpContext.Current.Application["OMNITURE_ACCOUNT"] == null)
                {
                    this.BuildConfiguration();

                    var accounts = HttpContext.Current.Application["OMNITURE_ACCOUNT"].ToString().Split(',', ';');

                    HttpContext.Current.Application["OMNITURE_ACCOUNT"] = string.Join(", ", accounts.Where(a => !string.IsNullOrEmpty(a))
                                                                                                    .Select(a => a.Trim())
                                                                                                    .ToArray());
                }
                return HttpContext.Current.Application["OMNITURE_ACCOUNT"].ToString();
            }
        }

        #endregion


        #region Configurations from Web.config
        public string ServerType
        {
            get
            {
                var asr = new AppSettingsReader();
                return asr.GetValue("ServerType", typeof(string)).ToString();
            }
        }

        public string Website
        {
            get
            {
                var asr = new AppSettingsReader();
                return asr.GetValue("Website", typeof(string)).ToString();
            }
        }

        public IList<string> GetSpecialBrands
        {
            get
            {
                var asr = new AppSettingsReader();

                return new List<string>
                   {
                       asr.GetValue("SOGoffCodeName", typeof (string)).ToString(),
                   };
            }
        }

        //public IList<string> GetPromoCategoryNames
        //{
        //    get
        //    {
        //        var asr = new AppSettingsReader();

        //        return new List<string>
        //           {
        //               //asr.GetValue("ClearanceCategoryName", typeof (string)).ToString(),
        //               asr.GetValue("OnsaleCategoryName", typeof (string)).ToString(),
        //           };
        //    }
        //}

        public IList<string> SpecialNotFreeShippingSkus
        {
            get
            {
                return new List<string>();
            }
        }

        #endregion
    }
}